package com.ruoyi.purchaseReturn.service.impl;

import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.WXShiroUtils;
import com.ruoyi.procurement.domain.ProcurementModel;
import com.ruoyi.procurement.domain.ProcurementOrder;
import com.ruoyi.procurement.mapper.ProcurementOrderMapper;
import com.ruoyi.product.domain.Product;
import com.ruoyi.purchaseReturn.domain.PurchaseReturnModel;
import com.ruoyi.purchaseReturn.domain.PurchaseReturnOrder;
import com.ruoyi.purchaseReturn.mapper.PurchaseReturnMapper;
import com.ruoyi.purchaseReturn.service.IPurchaseReturnService;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingMapper;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WangChangShuo
 * @date 2022年06月15日 11:25
 */
@Service
public class PurchaseReturnServiceImpl implements IPurchaseReturnService {
    @Autowired
    PurchaseReturnMapper purchaseReturnMapper;

    @Autowired
    WarehousingMapper warehousingMapper;

    @Autowired
    ProcurementOrderMapper procurementOrderMapper;

    @Autowired
    WarehousingRecordsMapper warehousingRecordsMapper;

    @Autowired
    AuditMapper auditMapper;

    @Override
    public List<PurchaseReturnOrder> selectPurchaseReturnOrder(PurchaseReturnOrder purchaseReturnOrder) {
        return purchaseReturnMapper.selectPurchaseReturnOrder(purchaseReturnOrder);
    }

    @Override
    public List<ProcurementOrder> selectProcurementOrderListByReturn(ProcurementOrder procurementOrder) {
        return purchaseReturnMapper.selectProcurementOrderListByReturn(procurementOrder);
    }

    @Override
    public List<ProcurementModel> selectProcurementModelById(HashMap hashMap) {
        return purchaseReturnMapper.selectProcurementModelById(hashMap);
    }

    @Override
    public ProcurementModel selectProcurementModels(HashMap<Object, Object> hashMap) {
        return purchaseReturnMapper.selectProcurementModels(hashMap);
    }

    @Override
    public ProcurementOrder selectProcurementOrderById(Long procurementWarehousingId) {
        return purchaseReturnMapper.selectProcurementOrderById(procurementWarehousingId);
    }

    @Override
    public int insertPurchaseReturnOrder(PurchaseReturnOrder purchaseReturnOrder) {
        return purchaseReturnMapper.insertPurchaseReturnOrder(purchaseReturnOrder);
    }

    @Override
    public int insertPurchaseReturnModel(PurchaseReturnOrder purchaseReturnOrder) {
        return purchaseReturnMapper.insertPurchaseReturnModel(purchaseReturnOrder);
    }

    @Override
    public int updatePurchaseIsReturn(Long purchaseId) {
        return purchaseReturnMapper.updatePurchaseIsReturn(purchaseId);
    }

    /**
     * 采购退货审核 后 修改库存
     *
     * @param
     */
    @Override
    @Transactional
    public void returnAudit(PurchaseReturnOrder purchaseReturnOrder) {

        //根据主键查询数据，判断是否含有产品规格型号子集
        PurchaseReturnOrder purchaseReturnOrder1 = purchaseReturnMapper.selectPurchaseReturnById(purchaseReturnOrder.getPurchaseReturnId());
        //如果驳回 修改订单状态
        if ("2".equals(purchaseReturnOrder.getPurchaseReturnStatus())) {
            purchaseReturnOrder.setAuditTime(DateUtils.getNowDate());
            purchaseReturnMapper.updatePurchaseReturn(purchaseReturnOrder);
            //修改原销货单状态 可以再选择单子进行采购退货
            Long procurementWarehousingId = purchaseReturnOrder1.getProcurementWarehousingId();

            ProcurementOrder procurementOrder = new ProcurementOrder();
            procurementOrder.setProcurementWarehousingId(procurementWarehousingId);
            procurementOrder.setIsReturn("0");
            procurementOrderMapper.updateProcurementOrder(procurementOrder);
        } else {
            if (purchaseReturnOrder1.getPurchaseReturnModels() != null) {
                //循环插入库存数据
                List<PurchaseReturnModel> purchaseReturnModels = purchaseReturnOrder1.getPurchaseReturnModels();
                WarehousingRecords warehousingRecords = new WarehousingRecords();

                for (PurchaseReturnModel purchaseReturnModel : purchaseReturnModels) {
                    int num = purchaseReturnModel.getReturnNum();
                    int cznum = purchaseReturnModel.getAllNum();

                    //找库存表 采购退货扣减库存
                    Warehousing warehousing = new Warehousing();
                    warehousing.setDeptId(purchaseReturnOrder1.getDeptId());
                    //根据 采购入库从表id 查采购入库
                    ProcurementModel procurementModel = purchaseReturnMapper.selectProcurementModelByNo(purchaseReturnModel.getProcurementWarehousingNo());
                    warehousing.setProductId(procurementModel.getProductId());
                    warehousing.setSelectId(procurementModel.getProductId());
                    warehousing.setProductType(procurementModel.getProductType());
                    warehousing.setBatch(procurementModel.getBatch());
                    Warehousing warehousing1 = warehousingMapper.selectWarehousing(warehousing);
                    //存储本次减少的数量
                    int returnNum = purchaseReturnModel.getReturnNum();

                    warehousingRecords.setInventoryNum(-returnNum);
                    warehousingRecords.setAllnum(-cznum);

                    //判断是否拥有该公司该产品该型号名称的库存记录
                    if (warehousing1 == null) {
                        warehousingMapper.insertWarehousing(warehousing);
                    } else {
                        Long warehousingNum = warehousing1.getAmount();
                        Long num1 = Long.valueOf(cznum);
                        num1 = warehousingNum - cznum;
                        //    warehousing.setWarehousingNum(num);
                        warehousing.setAmount(num1);
                        warehousing.setWarehousingId(warehousing1.getWarehousingId());
                        if (warehousing.getAmount() < 0) {
                            new AjaxResult(AjaxResult.Type.ERROR, "库存不足");
                            throw new RuntimeException("库存不足");
                        }
                        warehousingMapper.updateWarehousing(warehousing);
                    }

                    Long warehousingId = warehousing.getWarehousingId();
                    warehousingRecords.setWarehousingId(warehousingId);
                    //根据采购入库表id 查询客户id
                    ProcurementOrder procurementOrder = procurementOrderMapper.selectProcurementById(purchaseReturnOrder1.getProcurementWarehousingId());
                    warehousingRecords.setSupplierId(procurementOrder.getSupplierId());

                    //采购退货为5
                    warehousingRecords.setInventoryStatus("5");
                    warehousingRecords.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
                    warehousingRecordsMapper.insertWarehousingRecords(warehousingRecords);

                    PurchaseReturnOrder purchaseReturnOrder2 = new PurchaseReturnOrder();
                    purchaseReturnOrder2.setPurchaseReturnId(purchaseReturnOrder.getPurchaseReturnId());
                    purchaseReturnOrder2.setPurchaseReturnStatus("1");
                    purchaseReturnMapper.updatePurchaseReturn(purchaseReturnOrder2);
                }
            }
        }
    }

    @Override
    public PurchaseReturnOrder selectPurchaseReturnById(Long purchaseReturnId) {
        return purchaseReturnMapper.selectPurchaseReturnById(purchaseReturnId);
    }

    @Transactional
    @Override
    public int deletePurchaseReturnById(Long purchaseReturnId) {
        PurchaseReturnOrder purchaseReturnOrder = purchaseReturnMapper.selectPurchaseReturnById(purchaseReturnId);
        //删除 采购退货单 主表和子表信息
        int a = purchaseReturnMapper.deletePurchaseReturnOrder(purchaseReturnId);
        if (purchaseReturnOrder.getPurchaseReturnModels() != null) {
            int b = purchaseReturnMapper.deletePurchaseReturnModel(purchaseReturnId);
        }
        //删除 审核表 信息
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("orderId", purchaseReturnId);
        hashMap.put("orderType", "5");
        int c = auditMapper.deleteAuditWith(hashMap);
        //修改 采购入库单 istrue 状态

        int d = purchaseReturnMapper.updatePurchaseIsReturnZero(purchaseReturnOrder.getProcurementWarehousingId());

        if (a > 0 && c > 0 && d > 0) {
            return 1;
        } else {
            return 0;
        }

    }

    @Override
    public ProcurementModel selectProcurementModelByNo(Long procurementWarehousingNo) {
        return purchaseReturnMapper.selectProcurementModelByNo(procurementWarehousingNo);
    }


    @Override
    public List<ProcurementModel> selectProcurementModelListByNo(Long procurementWarehousingNo) {
        return purchaseReturnMapper.selectProcurementModelListByNo(procurementWarehousingNo);
    }

    @Override
    public int updatePurchaseReturnStatus(HashMap hashMap) {
        return purchaseReturnMapper.updatePurchaseReturnStatus(hashMap);
    }

    @Override
    public PurchaseReturnModel selectPurchaseReturnModelById(Long purchaseReturnModelId) {
        return purchaseReturnMapper.selectPurchaseReturnModelById(purchaseReturnModelId);
    }

    @Override
    public List<PurchaseReturnOrder> selectPurchaseReturnOrderListCustObj(Long companyId) {
        return purchaseReturnMapper.selectPurchaseReturnOrderListCustObj(companyId);
    }

    @Override
    public List<Product> selectPurchaseReturnModelByNo(Long purchaseReturnId) {
        return purchaseReturnMapper.selectPurchaseReturnModelByNo(purchaseReturnId);
    }

    @Override
    public List<PurchaseReturnOrder> selectProcurementSupplierObj(Long companyId) {
        return purchaseReturnMapper.selectProcurementSupplierObj(companyId);
    }

    @Override
    public int auditSale(PurchaseReturnOrder purchaseReturnOrder) {
        returnAudit(purchaseReturnOrder);
        return 1;
    }

    @Override
    public Long querypreviousId(Long purchaseReturnId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("purchaseReturnId", purchaseReturnId);
        params.put("deptId", SecurityUtils.getDeptId());
        return purchaseReturnMapper.querypreviousId(params);
    }

    @Override
    public Long queryNextId(Long purchaseReturnId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("purchaseReturnId", purchaseReturnId);
        params.put("deptId", SecurityUtils.getDeptId());
        return purchaseReturnMapper.queryNextId(params);
    }

    @Override
    public List<Map<String, Object>> pendingReview(Long deptID) {
        return purchaseReturnMapper.pendingReview(deptID)
                .stream()
                .peek(item -> item.put("orderType", "采购退货订单"))
                .collect(Collectors.toList());
    }
}
